package com.yikaiye.android.yikaiye.ui.fragment.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.base.BaseViewModel;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.UserModel;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

import java.util.HashMap;
import java.util.Map;

public class MineViewModel extends BaseViewModel {
    public MutableLiveData<UserModel> userModelMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> sendCodeSuccess = new MutableLiveData<>();
    public MutableLiveData<String> mergeSuccess = new MutableLiveData<>();
    public MutableLiveData<String> mergeError = new MutableLiveData<>();

    /**
     * 个人信息
     */
    public void getUserInfo() {
        addSubscribe(mRetrofitHelper.getUserInfo(MyApplication.manager.getUserDetailId()).subscribeWith(new BaseNetCallback<UserModel>() {
            @Override
            public void onSuccess(BaseModel<UserModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    userModelMutableLiveData.setValue(baseModel.getData());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }

    /**
     * 发送验证码
     */
    public void sendSmsCode(String phone) {
        addSubscribe(mRetrofitHelper.sendSmsCode(phone, MyApplication.manager.getUserId()).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    sendCodeSuccess.setValue(baseModel.getData());
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                ToastHelper.show(message);
            }
        }));
    }

    /**
     * 合并账号
     */
    public void mergeAccount(String phone, String smsCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("smsCode", smsCode);
        addSubscribe(mRetrofitHelper.bindingPhone(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {
            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    mergeSuccess.setValue(baseModel.getData());
                } else {
                    mergeError.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mergeError.setValue(message);
            }
        }));
    }
}
